package com.my.yyr.controller.safty;

import com.my.yyr.base.Constants;
import com.my.yyr.base.CurrUser;
import com.my.yyr.base.Result;
import com.my.yyr.dto.MenuDto;
import com.my.yyr.service.safty.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/safty/home")
public class HomeController {

    private final HomeService homeService;

    @Autowired
    public HomeController(HomeService homeService) {
        this.homeService = homeService;
    }

    @GetMapping("/curruser")
    public CurrUser currUser(HttpSession session){
        return (CurrUser)session.getAttribute(Constants.SESS_ATTR_CURRUSER);
    }

    @DeleteMapping("/curruser")
    public Result exit(HttpSession session){
        session.removeAttribute(Constants.SESS_ATTR_CURRUSER);
        return Result.success("您已退出系统！");
    }
    @GetMapping("/menu")
    public List<MenuDto> userMenus(HttpSession session){
        CurrUser currUser = (CurrUser) session.getAttribute(Constants.SESS_ATTR_CURRUSER);
        return homeService.getUserMenuList(currUser.getUserId());
    }
}
